﻿using MediatR;
using SheepCultivation.Common.Domain.main;
using SheepCultivation.Common.Domain.SheepBasicInformation;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.SheepFile
{
    public class SheepFileSeachHandler : IRequestHandler<SheepFileSeachCommon, Result>
    {
        public readonly IBaseRepository<BasicInformation> BasicInformationRepository;
        public readonly IBaseRepository<BirthMessage> BirthMessageRepository;
        public readonly IBaseRepository<Sheepbreed> SheepbreedRepository;
        public readonly IBaseRepository<BirthStatus> BirthStatusRepository;
        public readonly IBaseRepository<Transfer> TransferRepository;
        public readonly IBaseRepository<Building> BuildingRepository;
        public readonly IBaseRepository<Columns> ColumnsRepository;

        public SheepFileSeachHandler(IBaseRepository<BasicInformation> basicInformationRepository, IBaseRepository<BirthMessage> birthMessageRepository, IBaseRepository<Sheepbreed> sheepbreedRepository, IBaseRepository<BirthStatus> birthStatusRepository, IBaseRepository<Transfer> transferRepository, IBaseRepository<Building> buildingRepository, IBaseRepository<Columns> columnsRepository)
        {
            BasicInformationRepository = basicInformationRepository;
            BirthMessageRepository = birthMessageRepository;
            SheepbreedRepository = sheepbreedRepository;
            BirthStatusRepository = birthStatusRepository;
            TransferRepository = transferRepository;
            BuildingRepository = buildingRepository;
            ColumnsRepository = columnsRepository;
        }

        public async Task<Result> Handle(SheepFileSeachCommon request, CancellationToken cancellationToken)
        {
            var res = new Result() { Code = (int)CommonEnumCode.error };
            try
            {
                var list = (from a in BasicInformationRepository.GetAll()
                            join c in BirthMessageRepository.GetAll()
                          on a.BreedingSheepArchiveId equals c.BreedingSheepArchiveId
                            join d in SheepbreedRepository.GetAll() on a.SheepbreedId equals d.SheepbreedId
                            join e in BirthStatusRepository.GetAll() on c.BirthStatusId equals e.BirthStatusId
                            /*join g in BuildingRepository.GetAll() on a.BuildingId equals g.BuildingId
                            join h in ColumnsRepository.GetAll() on a.ColumnsId equals h.ColumnsId*/
                            where a.BuildingId == 0
                            select new BasicDto
                            {
                                Auricle = a.Auricle,
                                SheepbreedId = a.SheepbreedId,
                                CorpuscleAuricle = a.CorpuscleAuricle,
                                GeneLV = a.GeneLV,
                                SheepTypeId = a.SheepTypeId,
                                BirthMessageId = c.BirthMessageId,
                                SheepbreedName = d.SheepbreedName,
                                BreedingSheepArchiveId = a.BreedingSheepArchiveId,
                                DadBreedingSheepArchiveId = c.DadBreedingSheepArchiveId,
                                MomBreedingSheepArchiveId = c.MomBreedingSheepArchiveId,
                                DadEarNum = BasicInformationRepository.GetAll().Where(x => x.BreedingSheepArchiveId == c.DadBreedingSheepArchiveId).FirstOrDefault().Auricle,
                                MomEarNum = BasicInformationRepository.GetAll().Where(x => x.BreedingSheepArchiveId == c.MomBreedingSheepArchiveId).FirstOrDefault().Auricle,
                                BirthDate = c.BirthDate,
                                AdmissionDate = c.AdmissionDate,
                                BirthAdd = c.BirthAdd,
                                BirthWeight = c.BirthWeight,
                                Remark = c.Remark,
                                BirthStatusName = e.BirthStatusName,
                                SheepSex = a.SheepSex,
                                YearMD = DateTime.Now.ToString("yyyy-MM-dd"),
                                SheepStatus = a.SheepStatus
                                /*  BuildingName = g.BuildingName,
                                  ColumnsName = h.ColumnsName*/
                            }).ToList().Union(from a in BasicInformationRepository.GetAll()
                                              join c in BirthMessageRepository.GetAll()
                                            on a.BreedingSheepArchiveId equals c.BreedingSheepArchiveId
                                              join d in SheepbreedRepository.GetAll() on a.SheepbreedId equals d.SheepbreedId
                                              join e in BirthStatusRepository.GetAll() on c.BirthStatusId equals e.BirthStatusId
                                              join g in BuildingRepository.GetAll() on a.BuildingId equals g.BuildingId
                                              join h in ColumnsRepository.GetAll() on a.ColumnsId equals h.ColumnsId
                                              select new BasicDto
                                              {
                                                  Auricle = a.Auricle,
                                                  SheepbreedId = a.SheepbreedId,
                                                  CorpuscleAuricle = a.CorpuscleAuricle,
                                                  GeneLV = a.GeneLV,
                                                  SheepTypeId = a.SheepTypeId,
                                                  BirthMessageId = c.BirthMessageId,
                                                  SheepbreedName = d.SheepbreedName,
                                                  BreedingSheepArchiveId = a.BreedingSheepArchiveId,
                                                  DadBreedingSheepArchiveId = c.DadBreedingSheepArchiveId,
                                                  MomBreedingSheepArchiveId = c.MomBreedingSheepArchiveId,
                                                  DadEarNum = BasicInformationRepository.GetAll().Where(x => x.BreedingSheepArchiveId == c.DadBreedingSheepArchiveId).FirstOrDefault().Auricle,
                                                  MomEarNum = BasicInformationRepository.GetAll().Where(x => x.BreedingSheepArchiveId == c.MomBreedingSheepArchiveId).FirstOrDefault().Auricle,
                                                  BirthDate = c.BirthDate,
                                                  AdmissionDate = c.AdmissionDate,
                                                  BirthAdd = c.BirthAdd,
                                                  BirthWeight = c.BirthWeight,
                                                  Remark = c.Remark,
                                                  BirthStatusName = e.BirthStatusName,
                                                  SheepSex = a.SheepSex,
                                                  YearMD = DateTime.Now.ToString("yyyy-MM-dd"),
                                                  BuildingName = g.BuildingName,
                                                  ColumnsName = h.ColumnsName,
                                                  SheepStatus=a.SheepStatus

                                              }).ToList();

                if (request.BreedingSheepArchiveId != -1)
                {
                    list = list.Where(x => x.BreedingSheepArchiveId == request.BreedingSheepArchiveId).ToList();
                }
                res.Code = (int)CommonEnumCode.success;
                res.Data = list;
                res.Msg = "出生状态列表展示";
            }
            catch (Exception)
            {
                throw;
            }

            return res;
        }
    }
    public class BasicDto
    {
        public string Auricle { get; set; }
        /// <summary>
        /// 羊只品种Id
        /// </summary>
        public int SheepbreedId { get; set; }
        /// <summary>
        /// 电子耳号
        /// </summary>
        public string CorpuscleAuricle { get; set; }
        /// <summary>
        /// 基因等级
        /// </summary>
        public int GeneLV { get; set; }
        /// <summary>
        /// 羊只分类
        /// </summary>
        public int SheepTypeId { get; set; }
        public string? SheepbreedName { get; set; }
        public int BirthMessageId { get; set; }
        /// <summary>
        /// 基本信息Id
        /// </summary>
        public int BreedingSheepArchiveId { get; set; }
        /// <summary>
        /// 父亲耳号id
        /// </summary>
        public int DadBreedingSheepArchiveId { get; set; }
        /// <summary>
        /// 母亲耳号id
        /// </summary>
        public int MomBreedingSheepArchiveId { get; set; }
        /// <summary>
        /// 父亲耳号
        /// </summary>
        public string? DadEarNum { get; set; }
        /// <summary>
        /// 母亲耳号
        /// </summary>
        public string? MomEarNum { get; set; }
        /// <summary>
        /// 出生日期
        /// </summary>
        public DateTime BirthDate { get; set; }
        /// <summary>
        /// 入场日期
        /// </summary>
        public DateTime AdmissionDate { get; set; }
        /// <summary>
        /// 出生场地
        /// </summary>
        public string BirthAdd { get; set; }
        /// <summary>
        /// 出生重
        /// </summary>
        public decimal BirthWeight { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { get; set; }
        public string? BirthStatusName { get; set; }
        public int SheepSex { get; set; }
        public string YearMD { get; set; }
        public string? BuildingName { get; set; }
        public string? ColumnsName { get; set; }
        public int SheepStatus { get; set; }

    }
}
